home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Communication / Gazetteer / Source / util.c < prev    next >
C/C++ Source or Header  |  1991-11-10  |  6KB  |  270 lines

  1. /*
  2.  * A potpourri of handy little system utilities.
  3.  *
  4.  * Michael Hawley
  5.  * MIT Media Laboratory
  6.  * 20 Ames St
  7.  * Cambridge, MA 02139
  8.  * mike@media-lab.mit.edu
  9.  * Copyright (c) MIT Media Laboratory 1991
  10.  */
  11. #include "util.h"
  12. #include <sys/stat.h>
  13.  
  14. extern free(), access(), mkdir(), system(), 
  15.        strlen(), strcpy(), strcmp(), strncmp();
  16.  
  17. extern char *index(), *rindex();
  18.  
  19. char *av0, *_arg, *_argp; /* use by 'for_each_argument */
  20.  
  21. void
  22. error(a,b,c,d,e,f) int a,b,c,d,e,f; { /* printf an error msg */
  23.     printf((char *)a,b,c,d,e,f); printf("\n");
  24. }
  25.  
  26. int Verbose=0;
  27.  
  28. void
  29. debug(a,b,c,d) int a,b,c,d; { /* printf an error msg */
  30.     if (Verbose) printf((char *)a,b,c,d), printf("\n");
  31. }
  32.  
  33. int
  34. System(fmt, a,b,c,d,e,f,g)
  35.     char *fmt,*a,*b,*c,*d,*e,*f,*g;
  36. /*
  37.  * "printf" a system call, gripe if it failed.
  38.  */
  39. {
  40.     char t[2048];
  41.     int i=0;
  42.     sprintf(t,fmt,a,b,c,d,e,f,g);
  43.     debug("!%s",t);
  44.     if (i = system(t)) error("%s: failed! %s",av0,t);
  45.     return !i;
  46. }
  47.  
  48. char *
  49. save(s) char *s; { /* save a copy of 's' and return the pointer */
  50.     char *t = (char *)malloc(strlen(s)+1);
  51.     if (t) strcpy(t,s);
  52.     return t;
  53. }
  54.  
  55. int
  56. blank(s) char *s; { /* true if 's' is blank */
  57.     while (*s == ' ' || *s=='\t' || *s == '\n' || *s == '\r') ++s;
  58.     return (int)!*s;
  59. }
  60.  
  61. void
  62. stripcomment(s) char *s; { /* truncate 's' at a comment character ('#') */
  63.     char *p = index(s,'#');
  64.     if (p && (p==s || p[-1] != '\\')) *p = '\0';
  65. }
  66.  
  67. void
  68. stripnl(s) char *s; { /* remove trailing \n and space from 's' */
  69.     char *p = s + strlen(s)-1;
  70.     while (p>s && (*p=='\n' || *p == ' ' || *p=='\r')) *p-- = '\0';
  71. }
  72.  
  73. char *
  74. skipsp(s) char *s; {
  75.     while (*s==' ' || *s=='\t' || *s == '\n') ++s;
  76.     return s;
  77. }
  78.  
  79. void
  80. squishblank(s) char *s; {
  81.     char *t=s;
  82.     while (*t = *s){
  83.         if (t>s && (*t==' ' || *t=='\t') && (t[-1]==' '||t[-1]=='\t'))
  84.             ++s;
  85.         else
  86.             ++t, ++s;
  87.     }
  88. }
  89.  
  90. void
  91. squishwhite(s) char *s; {
  92.     char *t=s;
  93.     while (*t = *s){
  94.         if (t>s && (*t==' ' || *t=='\t' || *t=='\n') && 
  95.                    (t[-1]==' '||t[-1]=='\t'||t[-1]=='\n'))
  96.             ++s;
  97.         else
  98.             ++t, ++s;
  99.     }
  100. }
  101.  
  102. char *
  103. prefix(s,t) char *s, *t; { /* true if 't' is a prefix of 's' */
  104.     int sl = strlen(s);
  105.     t = skipsp(t);
  106.     return (*t == *s && strncmp(s,t,sl)==0)? skipsp(t+sl) : (char *)0;
  107. }
  108.  
  109. int
  110. suffix(s,t) char *s, *t; { /* true if 't' is a suffix of 's' */
  111.     s = rindex(s,'.');
  112.     return s? strcmp(s,t)==0 : 0;
  113. }
  114.  
  115. char *
  116. strindex(s,t) char *s, *t; { /* return ptr to first match of 't' in 's' */
  117.     int n = strlen(t);
  118.  
  119.     if (s)
  120.         while (*s)
  121.             if (!strncmp(s, t, n))
  122.                 return s;
  123.             else
  124.                 s++;
  125.     return (char *)0;
  126. }
  127.  
  128. void
  129. sub(s,a,b) char *s,a,b; { /* in 's', s/a/b/g */
  130.     while (*s){
  131.         if (*s==a) *s=b;
  132.         s++;
  133.     }
  134. }
  135.  
  136. void
  137. substr(s,a,b) char *s, *a, *b; { /* like 'sub', but with strings */
  138.     char q[8192];
  139.     char *p = s;
  140.     int n = strlen(a);
  141.     for (;*p;p++){
  142.         if (*p == *a && strncmp(p,a,n)==0){
  143.             strcpy(q,p+n);
  144.             strcpy(p,b);
  145.             strcpy(p+strlen(b),q);
  146.             p += strlen(b)-1;
  147.         }
  148.     }
  149. }
  150.  
  151. void
  152. stot(s,t,c) char *s, **t, c; { /* split 's' into a table 't' */
  153.     char *p;
  154.     while (p = index(s,c)){
  155.         *p++ = '\0';
  156.         *t++ = s;
  157.         s = p;
  158.     }
  159.     *t++ = s;
  160.     *t = (char *)0;
  161. }
  162.  
  163. #include <sys/stat.h>
  164.  
  165. int fMode(f) char *f; { /* return mode bits */
  166.     struct stat b;
  167.     return stat(f,&b)? 0 : b.st_mode;
  168. }
  169.  
  170. int fSize(f) char *f; { /* return mode bits */
  171.     struct stat b;
  172.     return stat(f,&b)? 0 : b.st_size;
  173. }
  174.  
  175. int fTime(f) char *f; { /* mod time of file 'f' */
  176.     struct stat b;
  177.     return stat(f,&b)? 0 : b.st_mtime;
  178. }
  179.  
  180. int fDirectory(f) char *f; { /* true if file 'f' is a directory */
  181.     struct stat b;
  182.     return stat(f,&b)? 0 : ((b.st_mode & S_IFDIR) == S_IFDIR);
  183. }
  184.  
  185. int fLink(f) char *f; { /* true if file 'f' is a symbolic link */
  186.     struct stat b;
  187.     return lstat(f,&b)? 0 : ((b.st_mode & S_IFLNK) == S_IFLNK);
  188. }
  189.  
  190. int
  191. mkdirs(s,mode)
  192.     char *s;
  193.     int mode;
  194. { /* ensure that all the directories in 's' exist */
  195.     char t[4096], *p = t;
  196.  
  197.     strcpy(t,s);
  198.     if (*p=='/') ++p;
  199.     while (p = index(p,'/')) {
  200.     *p = '\0';
  201.         if (access(t,0)){
  202.             if (mkdir(t,mode))
  203.                 return 0;
  204.         /* fixmod(t); */
  205.     }
  206.         *p++ = '/';
  207.     }
  208.     if (access(t,0) && mkdir(t,mode)) return 0;
  209.     /* fixmod(t); */
  210.     debug("mkdir %s",t);
  211.     return 1;
  212. }
  213.  
  214. #ifdef ultrix
  215.  
  216. /* Ultrix version */
  217.  
  218. char *re_comp();
  219.  
  220. match(s,expr)
  221.     char *s, *expr;
  222. /*
  223.  * true if 's' matches regular expression 'expr'
  224.  * (a simple regex, grep-style, *not* egrep)
  225.  */
  226. {
  227.     static char p[256]="", *q=(char *)0;
  228.     static struct regex *r = (struct regex *)0;
  229.     if (q != expr){
  230.         if (strcmp(p,expr)){
  231.             if (r) free(r);
  232.             if (re_comp(expr))
  233.                 ;
  234.             else return 0;
  235.             strcpy(p,expr);
  236.         }
  237.         q = expr;
  238.     }
  239.     return re_exec(s,r)==1? 1: 0;
  240. }
  241. #else
  242.  
  243. /* NeXT version */
  244.  
  245. #include <regex.h>
  246.  
  247. int
  248. match(s,expr)
  249.     char *s, *expr;
  250. /*
  251.  * true if 's' matches regular expression 'expr'
  252.  * (a simple regex, grep-style, *not* egrep)
  253.  */
  254. {
  255.     static char p[256]="", *q=(char *)0;
  256.     static struct regex *r = (struct regex *)0;
  257.     if (q != expr){
  258.         if (strcmp(p,expr)){
  259.             if (r) free(r);
  260.             if (r=re_compile(expr,0))
  261.                 ;
  262.             else return 0;
  263.             strcpy(p,expr);
  264.         }
  265.         q = expr;
  266.     }
  267.     return r? re_match(s,r)==1 : 0;
  268. }
  269. #endif
  270.